package pl.edu.agh.mr.nb.server;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Topic;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;

import pl.edu.agh.mr.nb.node.ComputationOrderMsg;

public class Node {

	public String nodeName;
	public int nodeStatus; 
	public TopicPublisher publisher;
	public TopicSession session;
	public Topic topic;
	
	public Node(String nodeName, TopicSession session, String topicPrefix) throws JMSException {
		this.nodeName = nodeName;
		this.nodeStatus = NodeStatus.IDLE;
		this.session = session;
		this.topic = session.createTopic(topicPrefix+"/"+nodeName);
		this.publisher = session.createPublisher(this.topic);
	}

	public void sendOrder(ComputationOrderMsg order) throws JMSException {
	
		System.out.println("-> Trying to send an order message");
		ObjectMessage message = session.createObjectMessage();
		System.out.println("-> message created");
		message.setObject(order);
		System.out.println("-> object set");
		publisher.publish(message);
		System.out.println("-> message published");
	}
}
